package service

import (
	"context"

	"gitee.com/WisdomClassroom/core/models"
	"gitee.com/WisdomClassroom/core/protobufs/v1/pb"
	"gitee.com/WisdomClassroom/knowledgePoint/glb"
)

func (s Service) ListChapterKnowledgePoints(ctx context.Context, request *pb.ListChapterKnowledgePointsRequest) (*pb.ListChapterKnowledgePointsResponse, error) {
	knowledgePoints := make([]models.KnowledgePoint, 0, 32)
	err := glb.DB.Find(&knowledgePoints, "chapter_uuid = ?", request.GetChapterUUID()).Error
	if err != nil {
		glb.Logger.Error(err.Error())
		return listChapterKnowledgePointRespSvrErr, nil
	}

	knowledgePointsResp := make([]*pb.KnowledgePointBasicInfo, 0, len(knowledgePoints))
	for _, knowledgePoint := range knowledgePoints {
		knowledgePointsResp = append(knowledgePointsResp, &pb.KnowledgePointBasicInfo{
			UUID: knowledgePoint.UUID,
			Name: knowledgePoint.Name,
		})
	}

	return &pb.ListChapterKnowledgePointsResponse{
		Status:          &pb.ResponseStatus{Code: pb.ResponseStatusCode_Success},
		KnowledgePoints: knowledgePointsResp,
	}, nil
}
